package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.awt.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 山顶洞人郑某
 * @className categoryServiceImpl.java
 * @date 2024 01 19
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        category.setStatus(0);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentEmpId());
        category.setUpdateUser(BaseContext.getCurrentEmpId());
        categoryMapper.add(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> getByType(Integer type) {
        List<Category> list = categoryMapper.getBtyType(type);
        return list;
    }

    @Override
    public void update(Category category) {
        category.setUpdateUser(BaseContext.getCurrentEmpId());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO 分类分类查询参数
     * @return 分页结果
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
      PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
      Page<Category> page =  categoryMapper.page(categoryPageQueryDTO);
      PageResult pageResult = new PageResult();
      pageResult.setRecords(page.getResult());
      pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    /**
     * 分类启用和禁用
     *
     * @param status
     * @param id
     */
    @Override
    public void setStatus(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        category.setUpdateUser(BaseContext.getCurrentEmpId());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }




    /**
     * 根据id删除分类
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }

    /**
     * 查询分类信息
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type, StatusConstant.ENABLE);
    }
}
